// 小男孩左右手事件绑定
var hand_l = document.querySelector(".hand-l");
var hand_r = document.querySelector(".hand-r");
var pw = document.querySelector("input[type='password']");

pw.addEventListener('focus', function () {
    hand_l.className = 'hand-l-focus';
    hand_r.className = 'hand-r-focus';
})
pw.addEventListener('blur', function () {
    hand_l.className = 'hand-l';
    hand_r.className = 'hand-r';
})

// 点击返回顶部的事件
var back = document.querySelector(".back")
back.addEventListener('click',function (){
    history.back()
})

// 点击登录按钮的事件
var submit = document.querySelector("input[value='登录']")
var username = document.querySelector("input[name='U-name']")
submit.addEventListener('click',function() {
    if(pw.value == "" || username.value == ""){
        alert("请将信息填写完整")
        return
    }
    $.ajax({
        url:"user/login",
        type:"post",
        data:JSON.stringify({
            username: username.value,
            password: pw.value,
        }),
        contentType:"application/json;charset=UTF-8",
        dataType:"json",
        success:function (data){
            console.log(data)
            if(data.username == "none"){
                alert("用户名/密码错误!")
            }else {
                alert("登录成功!")
                location.href = "/chapter12/"
                //保存cookie
                setCookie({
                    username:data.username
                },7)
            }
        }
    })
})